<?php
	require_once 'models/App.php';
	require_once 'models/Event.php';
	require_once 'models/Menu.php';
	require_once 'models/Cell.php';
	require_once 'models/Subsite.php';
	require_once 'DBConnect.php';

	class AppController
	{
		
		public function __construct() {

		}
		
		//TODO: remove test case
		public function getApp($eventID) {
			
			//first we need access to the database...
			$connection = DBConnect::getInstance();
			
			if(! $connection->connect()) {
				//TODO: Handle error in connecting better
				return 'Connection Error (from AppController.GetApp())';
			}
			else {
				//now we are connected to the database!
				
				//An App-object needs an Event-object and a Menu-object..
				$event = $connection->fetchEvent($eventID);
				
				$menu = $connection->fetchMenu($event->ID);
				
				$app = new App($event, $menu);
				return $app;
			}
		}
		
		public function getEvent($eventID) {
			$connection = DBConnect::getInstance();
			
			if(! $connection->connect()) {
				//TODO: Handle error in connecting better
				return 'Connection Error (from AppController.getEvent())';
			}
			else {
				return $connection->fetchEvent($eventID);
			}
		}
		
		//returns the ID of the saved subsite (created by DB)
		public function saveSubsite($html) {
			$connection = DBConnect::getInstance();
			
			if(! $connection->connect()) {
				//TODO: Handle error in connecting better
				return 'Connection Error (from AppController.saveSubsite()';
			}
			else {
				return $connection->saveSubsite($html);
			}
		}
		
		//returns the ID of the saved cell (created by DB)
		public function saveCell($eventID, $btnText, $subsiteID, $posX, $posY) {
			$connection = DBConnect::getInstance();
			
			if(! $connection->connect()) {
				//TODO: Handle error in connecting better
				return 'Connection Error (from AppController.saveCell()';
			}
			else {
				return $connection->saveCell($eventID, $btnText, $subsiteID, $posX, $posY);
			}
		}
	}
	
	if(isset($_GET['debug'])) {
		//inset debug tests here..
	}
?>